# Copyright 2010-2025 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_python//python:proto.bzl", "py_proto_library")

package(default_visibility = ["//visibility:public"])

cc_library(
    name = "scheduler",
    srcs = ["scheduler.cc"],
    hdrs = ["scheduler.h"],
    deps = [
        ":solvers_cc_proto",
        "//ortools/base:threadpool",
        "@com_google_absl//absl/functional:any_invocable",
        "@eigen",
    ],
)

cc_test(
    name = "scheduler_test",
    srcs = ["scheduler_test.cc"],
    deps = [
        ":gtest_main",
        ":scheduler",
        ":solvers_cc_proto",
        "@com_google_absl//absl/functional:any_invocable",
    ],
)

proto_library(
    name = "solve_log_proto",
    srcs = ["solve_log.proto"],
    deps = [":solvers_proto"],
)

cc_proto_library(
    name = "solve_log_cc_proto",
    deps = [":solve_log_proto"],
)

py_proto_library(
    name = "solve_log_py_pb2",
    deps = [":solve_log_proto"],
)

proto_library(
    name = "solvers_proto",
    srcs = ["solvers.proto"],
    deps = ["//ortools/glop:parameters_proto"],
)

cc_proto_library(
    name = "solvers_cc_proto",
    deps = [":solvers_proto"],
)

py_proto_library(
    name = "solvers_py_pb2",
    deps = [
        ":solvers_proto",
    ],
)

cc_library(
    name = "gtest_main",
    srcs = ["gtest_main.cc"],
    deps = [
        "//ortools/base",
        "//ortools/base:gmock",
    ],
)

cc_library(
    name = "iteration_stats",
    srcs = ["iteration_stats.cc"],
    hdrs = ["iteration_stats.h"],
    deps = [
        ":quadratic_program",
        ":sharded_quadratic_program",
        ":sharder",
        ":solve_log_cc_proto",
        ":solvers_cc_proto",
        "//ortools/base",
        "//ortools/base:mathutil",
        "@com_google_absl//absl/random:distributions",
        "@eigen",
    ],
)

cc_test(
    name = "iteration_stats_test",
    srcs = ["iteration_stats_test.cc"],
    deps = [
        ":gtest_main",
        ":iteration_stats",
        ":quadratic_program",
        ":sharded_quadratic_program",
        ":solve_log_cc_proto",
        ":solvers_cc_proto",
        ":test_util",
        "//ortools/base:protobuf_util",
        "@eigen",
    ],
)

cc_library(
    name = "primal_dual_hybrid_gradient",
    srcs = ["primal_dual_hybrid_gradient.cc"],
    hdrs = ["primal_dual_hybrid_gradient.h"],
    deps = [
        ":iteration_stats",
        ":quadratic_program",
        ":sharded_optimization_utils",
        ":sharded_quadratic_program",
        ":sharder",
        ":solve_log_cc_proto",
        ":solvers_cc_proto",
        ":solvers_proto_validation",
        ":termination",
        ":trust_region",
        "//ortools/base",
        "//ortools/base:mathutil",
        "//ortools/base:timer",
        "//ortools/glop:parameters_cc_proto",
        "//ortools/glop:preprocessor",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "//ortools/lp_data",
        "//ortools/lp_data:base",
        "//ortools/lp_data:proto_utils",
        "//ortools/util:logging",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
        "@com_google_protobuf//:protobuf",
        "@eigen",
    ],
)

cc_test(
    name = "primal_dual_hybrid_gradient_test",
    size = "medium",
    srcs = ["primal_dual_hybrid_gradient_test.cc"],
    shard_count = 3,
    deps = [
        ":gtest_main",
        ":iteration_stats",
        ":primal_dual_hybrid_gradient",
        ":quadratic_program",
        ":quadratic_program_io",
        ":sharded_quadratic_program",
        ":solve_log_cc_proto",
        ":solvers_cc_proto",
        ":termination",
        ":test_util",
        "//ortools/base",
        "//ortools/glop:parameters_cc_proto",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "//ortools/lp_data",
        "//ortools/lp_data:base",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@eigen",
    ],
)

cc_library(
    name = "quadratic_program",
    srcs = ["quadratic_program.cc"],
    hdrs = ["quadratic_program.h"],
    deps = [
        "//ortools/base",
        "//ortools/base:status_macros",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@eigen",
    ],
)

cc_test(
    name = "quadratic_program_test",
    size = "small",
    srcs = ["quadratic_program_test.cc"],
    deps = [
        ":gtest_main",
        ":quadratic_program",
        ":test_util",
        "//ortools/base:protobuf_util",
        "//ortools/base:status_macros",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@eigen",
    ],
)

cc_library(
    name = "quadratic_program_io",
    srcs = ["quadratic_program_io.cc"],
    hdrs = ["quadratic_program_io.h"],
    deps = [
        ":quadratic_program",
        "//ortools/base",
        "//ortools/base:mathutil",
        "//ortools/base:status_macros",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "//ortools/linear_solver:model_exporter",
        "//ortools/lp_data:mps_reader_template",
        "//ortools/util:file_util",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@eigen",
    ],
)

cc_library(
    name = "sharded_optimization_utils",
    srcs = ["sharded_optimization_utils.cc"],
    hdrs = ["sharded_optimization_utils.h"],
    deps = [
        ":quadratic_program",
        ":sharded_quadratic_program",
        ":sharder",
        ":solve_log_cc_proto",
        "//ortools/base",
        "//ortools/base:mathutil",
        "@com_google_absl//absl/random:distributions",
        "@eigen",
    ],
)

cc_test(
    name = "sharded_optimization_utils_test",
    size = "small",
    srcs = ["sharded_optimization_utils_test.cc"],
    deps = [
        ":gtest_main",
        ":quadratic_program",
        ":sharded_optimization_utils",
        ":sharded_quadratic_program",
        ":sharder",
        ":solve_log_cc_proto",
        ":test_util",
        "@eigen",
    ],
)

cc_library(
    name = "sharded_quadratic_program",
    srcs = ["sharded_quadratic_program.cc"],
    hdrs = ["sharded_quadratic_program.h"],
    deps = [
        ":quadratic_program",
        ":scheduler",
        ":sharder",
        ":solvers_cc_proto",
        "//ortools/base",
        "//ortools/util:logging",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/strings",
        "@eigen",
    ],
)

cc_test(
    name = "sharded_quadratic_program_test",
    srcs = ["sharded_quadratic_program_test.cc"],
    deps = [
        ":gtest_main",
        ":quadratic_program",
        ":sharded_quadratic_program",
        ":sharder",
        ":test_util",
        "@eigen",
    ],
)

cc_library(
    name = "sharder",
    srcs = ["sharder.cc"],
    hdrs = ["sharder.h"],
    deps = [
        ":scheduler",
        "//ortools/base",
        "//ortools/base:mathutil",
        "//ortools/base:timer",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@eigen",
    ],
)

cc_test(
    name = "sharder_test",
    size = "small",
    srcs = ["sharder_test.cc"],
    deps = [
        ":gtest_main",
        ":scheduler",
        ":sharder",
        "//ortools/base",
        "//ortools/base:mathutil",
        "@com_google_absl//absl/random:distributions",
        "@eigen",
    ],
)

cc_library(
    name = "solvers_proto_validation",
    srcs = ["solvers_proto_validation.cc"],
    hdrs = ["solvers_proto_validation.h"],
    deps = [
        ":solvers_cc_proto",
        "//ortools/base:status_macros",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "solvers_proto_validation_test",
    srcs = ["solvers_proto_validation_test.cc"],
    deps = [
        ":gtest_main",
        ":solvers_cc_proto",
        ":solvers_proto_validation",
        "//ortools/base:protobuf_util",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "termination",
    srcs = ["termination.cc"],
    hdrs = ["termination.h"],
    deps = [
        ":solve_log_cc_proto",
        ":solvers_cc_proto",
        "//ortools/base",
    ],
)

cc_test(
    name = "termination_test",
    size = "small",
    srcs = ["termination_test.cc"],
    deps = [
        ":gtest_main",
        ":solve_log_cc_proto",
        ":solvers_cc_proto",
        ":termination",
        "//ortools/base:protobuf_util",
    ],
)

cc_library(
    name = "test_util",
    testonly = 1,
    srcs = ["test_util.cc"],
    hdrs = ["test_util.h"],
    deps = [
        ":quadratic_program",
        "//ortools/base",
        "//ortools/base:gmock",
        "@com_google_absl//absl/types:span",
        "@eigen",
    ],
)

cc_test(
    name = "test_util_test",
    srcs = ["test_util_test.cc"],
    deps = [
        ":gtest_main",
        ":test_util",
        "//ortools/base",
        "@com_google_absl//absl/types:span",
        "@eigen",
    ],
)

cc_library(
    name = "trust_region",
    srcs = ["trust_region.cc"],
    hdrs = ["trust_region.h"],
    deps = [
        ":quadratic_program",
        ":sharded_optimization_utils",
        ":sharded_quadratic_program",
        ":sharder",
        "//ortools/base",
        "//ortools/base:mathutil",
        "@com_google_absl//absl/algorithm:container",
        "@eigen",
    ],
)

cc_test(
    name = "trust_region_test",
    srcs = ["trust_region_test.cc"],
    deps = [
        ":gtest_main",
        ":quadratic_program",
        ":sharded_optimization_utils",
        ":sharded_quadratic_program",
        ":sharder",
        ":test_util",
        ":trust_region",
        "@com_google_absl//absl/strings",
        "@eigen",
    ],
)
